package com.sh.caryr.erp.controller.basis;


import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sh.caryr.erp.entity.PageResultMessage;
import com.sh.caryr.erp.entity.ResultMessage;
import com.sh.caryr.erp.exception.ResultCode;
import com.sh.caryr.erp.entity.basis.Shop;
import com.sh.caryr.erp.entity.user.User;
import com.sh.caryr.erp.exception.RestServiceException;
import com.sh.caryr.erp.service.IShopService;
import com.sh.caryr.erp.util.ERPConstants;
import com.sh.caryr.erp.util.MyBatisUtil;
import com.sh.caryr.erp.util.UtilityTool;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 店铺表 前端控制器
 * </p>
 *
 * @author milkeeper
 * @since 2017-11-22
 */
@RestController
@RequestMapping("/api/shop")
@SessionAttributes("user")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @ApiOperation(value = "获取shop列表信息", notes = "获取shop列表信息")
    @RequestMapping(method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<List<Shop>> findShops(@RequestParam(defaultValue = "1") Integer current,
                                               @RequestParam(defaultValue = "20") Integer size,
                                               @RequestParam(required = false) String queryField,
                                               @RequestParam(required = false) String queryValue,
                                               @RequestParam(required = false) Boolean isAccurate,
                                               @RequestParam(defaultValue = "id") String orderBy,
                                               @RequestParam(defaultValue = "false") Boolean isAsc,
                                               @ModelAttribute("user") User operator) {
        Wrapper wrapper = MyBatisUtil.getIsActiveWrapper(queryField, queryValue, orderBy,isAsc);
        MyBatisUtil.addAuthFilter(wrapper, operator, ERPConstants.AUTH_TARGET_SHOP, false, false);
        List<Shop> shops = shopService.findShopsDynamic(current, size, wrapper, orderBy);
        return new PageResultMessage<>(shops, shopService.selectCount(wrapper));
    }

    @ApiOperation(value = "获取单个shop信息", notes = "获取单个shop信息")
    @ApiImplicitParam(name = "id", value = "shopId", required = true, dataType = "long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage<Shop> findShopById(@PathVariable long id) {
        return new ResultMessage<>(shopService.selectById(id));
    }

    @ApiOperation(value = "创建shop", notes = "根据shop对象创建shop")
    @ApiImplicitParam(name = "shop", value = "shop详细实体", required = true, dataType = "Shop")
    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public Object insertShop(@RequestBody Shop shop, @ModelAttribute("user") User operator) {
        shop.setCreateBy(operator.getId());
        shop.setCoco(operator.getCoco());
        if (!shopService.insert(shop)) {
            throw new RestServiceException(ResultCode.CREATE_FAILED);
        }
        return new ResultMessage<>(shop.getId().toString());
    }

    @ApiOperation(value = "更新shop", notes = "根据shop对象更新shop")
    @ApiImplicitParam(name = "shop", value = "shop详细实体", required = true, dataType = "Shop")
    @RequestMapping(method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    public Object updateShopById(@RequestBody Shop shop, @ModelAttribute("user") User operator) {
        shop.setUpdateBy(operator.getId());
        shop.setUpdateAt(new Date());
        if (!shopService.updateById(shop)) {
            throw new RestServiceException(ResultCode.UPDATE_FAILED);
        }
        return new ResultMessage<>(shop);
    }

    @ApiOperation(value = "批量删除shop", notes = "根据url的id来指定删除shop")
    @ApiImplicitParam(name = "ids", value = "shopIds", required = true, dataType = "String")
    @Transactional
    @RequestMapping(value = "/{ids}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public ResultMessage deleteShops(@PathVariable String ids) {
        List<Long> list = UtilityTool.convertStringArray2LongList(ids.split(","));
        softDelete(list);
        return new ResultMessage();
    }


    /**
     * 软删除，isActive = 0
     *
     * @param list
     */
    private void softDelete(List<Long> list) {
        List<Shop> shops = shopService.selectBatchIds(list);
        for (Shop shop : shops) {
            shop.setIsActive(ERPConstants.ENITYT_INACTIVE);
        }
        if (!shopService.updateBatchById(shops)) {
            throw new RestServiceException(ResultCode.UPDATE_FAILED);
        }
    }
}
